package club.gongsir.mapreduce;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author 码之泪殇
 * @date 2020/6/22 20:39
 * 编码不要畏惧变化，要拥抱变化
 */
public class RatingTuple implements WritableComparable<RatingTuple> {
    /**
     * 国家/地区
     */
    private Text city = new Text();

    /**
     * 评分
     */
    private FloatWritable score = new FloatWritable();

    /**
     * getter和setter方法
     */
    public Text getCity() {
        return city;
    }

    public void setCity(Text city) {
        this.city = city;
    }

    public FloatWritable getScore() {
        return score;
    }

    public void setScore(FloatWritable score) {
        this.score = score;
    }

    @Override
    public int compareTo(RatingTuple o) {
        // 先按照 国家名称 升序排列
        int cmp = this.getCity().compareTo(o.getCity());
        if (cmp != 0) {
            return cmp;
        }
        // 国家名称相同,则按照评分降序排列
        return -this.getScore().compareTo(o.getScore());
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        city.write(dataOutput);
        score.write(dataOutput);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        city.readFields(dataInput);
        score.readFields(dataInput);
    }
}
